package drools.test;

import org.apache.commons.io.FileUtils;
import org.drools.core.impl.KnowledgeBaseFactory;
import org.junit.Test;
import org.kie.api.KieBase;
import org.kie.api.KieBaseConfiguration;
import org.kie.api.KieServices;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.StatelessKieSession;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderError;
import org.kie.internal.builder.KnowledgeBuilderErrors;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.utils.KieHelper;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class DroolsDemo {

    /**
     * 加载配置文件方式
     */
    @Test
    public void testDrlFile() {

        KieServices ks = KieServices.Factory.get();

        KieContainer kieContainer = ks.getKieClasspathContainer();
        KieSession kSession = kieContainer.newKieSession("ksession-rule");

        Product product = new Product();
        product.setType(Product.GOLD);

        kSession.insert(product);
        int count = kSession.fireAllRules();
        System.out.println("命中了" + count + "条规则");
        System.out.println("商品" + product.getType() + "的商品折扣为" + product.getDiscount() + "%");
    }


    /**
     * 通过字符串动态加载
     *
     * @throws Exception
     */
    @Test
    public void testDrlString() throws Exception {

        KieServices ks = KieServices.Factory.get();
        KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
        String s = FileUtils.readFileToString(new File("D:\\Rules.drl"), "utf-8");
        kb.add(ResourceFactory.newByteArrayResource(s.getBytes("utf-8")), ResourceType.DRL);
        KnowledgeBuilderErrors errors = kb.getErrors();
        for (KnowledgeBuilderError error : errors) {
            System.out.println(error);
        }
        KieBase kieBase = kb.newKieBase();
        KieSession kSession = kieBase.newKieSession(ks.newKieSessionConfiguration(), ks.newEnvironment());
        Product product = new Product();
        product.setType(Product.GOLD);
        kSession.insert(product);
        int count = kSession.fireAllRules();
        System.out.println("命中了" + count + "条规则");
        System.out.println("商品" + product.getType() + "的商品折扣为" + product.getDiscount() + "%");
    }


    /**
     * 加载字符串，新api
     */
    @Test
    public void testNewApiLoadString() throws IOException {

        String s = FileUtils.readFileToString(new File("D:\\Rules.drl"), "utf-8");
        KieHelper helper = new KieHelper();
        helper.addContent(s, ResourceType.DRL);
        KieSession kSession = helper.build().newKieSession();

        Product product = new Product();
        product.setType(Product.GOLD);

        kSession.insert(product);
        int count = kSession.fireAllRules();

        kSession.dispose();

        System.out.println("命中了" + count + "条规则");
        System.out.println("商品" + product.getType() + "的商品折扣为" + product.getDiscount() + "%");

    }


}
